package com.evaluation.kexingcp.psycho.evaluation.sys.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.evaluation.kexingcp.common.core.util.R;
import com.evaluation.kexingcp.common.excel.annotation.ResponseExcel;
import com.evaluation.kexingcp.common.log.annotation.SysLog;
import com.evaluation.kexingcp.psycho.evaluation.sys.entity.EvaluationTeamReportTemplatesEntity;
import com.evaluation.kexingcp.psycho.evaluation.sys.service.EvaluationTeamReportTemplatesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 团体报告模版表
 *
 * @author pig
 * @date 2024-09-06 16:27:49
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/evaluationTeamReportTemplates" )
@Tag(description = "evaluationTeamReportTemplates" , name = "团体报告模版表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class EvaluationTeamReportTemplatesController {

    private final  EvaluationTeamReportTemplatesService evaluationTeamReportTemplatesService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param evaluationTeamReportTemplates 团体报告模版表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('pigx_evaluationTeamReportTemplates_view')")
    public R getEvaluationTeamReportTemplatesPage(@ParameterObject Page page, @ParameterObject EvaluationTeamReportTemplatesEntity evaluationTeamReportTemplates) {
        LambdaQueryWrapper<EvaluationTeamReportTemplatesEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(evaluationTeamReportTemplates.getTemplateType()),
                EvaluationTeamReportTemplatesEntity::getTemplateType,evaluationTeamReportTemplates.getTemplateType());
        return R.ok(evaluationTeamReportTemplatesService.page(page, wrapper));
    }


    /**
     * 通过id查询团体报告模版表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('pigx_evaluationTeamReportTemplates_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(evaluationTeamReportTemplatesService.getById(id));
    }

    /**
     * 新增团体报告模版表
     * @param evaluationTeamReportTemplates 团体报告模版表
     * @return R
     */
    @Operation(summary = "新增团体报告模版表" , description = "新增团体报告模版表" )
    @SysLog("新增团体报告模版表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('pigx_evaluationTeamReportTemplates_add')" )
    public R save(@RequestBody EvaluationTeamReportTemplatesEntity evaluationTeamReportTemplates) {
        return R.ok(evaluationTeamReportTemplatesService.save(evaluationTeamReportTemplates));
    }

    /**
     * 修改团体报告模版表
     * @param evaluationTeamReportTemplates 团体报告模版表
     * @return R
     */
    @Operation(summary = "修改团体报告模版表" , description = "修改团体报告模版表" )
    @SysLog("修改团体报告模版表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('pigx_evaluationTeamReportTemplates_edit')" )
    public R updateById(@RequestBody EvaluationTeamReportTemplatesEntity evaluationTeamReportTemplates) {
        return R.ok(evaluationTeamReportTemplatesService.updateById(evaluationTeamReportTemplates));
    }

    /**
     * 通过id删除团体报告模版表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除团体报告模版表" , description = "通过id删除团体报告模版表" )
    @SysLog("通过id删除团体报告模版表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('pigx_evaluationTeamReportTemplates_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(evaluationTeamReportTemplatesService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param evaluationTeamReportTemplates 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('pigx_evaluationTeamReportTemplates_export')" )
    public List<EvaluationTeamReportTemplatesEntity> export(EvaluationTeamReportTemplatesEntity evaluationTeamReportTemplates,Long[] ids) {
        return evaluationTeamReportTemplatesService.list(Wrappers.lambdaQuery(evaluationTeamReportTemplates).in(ArrayUtil.isNotEmpty(ids), EvaluationTeamReportTemplatesEntity::getId, ids));
    }
}